package org.go.smart;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class GoSmart extends HttpServlet{
    
    public void init(ServletConfig config) throws ServletException {
    	super.init(config);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doGet(request, response);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String heaterStatus = request.getParameter("heaterStatus");
    	if (heaterStatus != null) {
    		response.setStatus(HttpServletResponse.SC_OK);
    		response.setContentType("text/html");
    		ServletOutputStream out = response.getOutputStream();
    		String status = HouseController.getInstance().isHeaterActive() ? "active" : "inactive";
    		out.println(status);
    		out.flush();
    	} else {
    		response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    		response.getWriter().append("Request not understood by server!");
    	}
    }
    
}
